package com.sl.au.service.impl;


import com.sl.au.entity.Direction;
import com.sl.au.repository.DirectionRepository;
import com.sl.au.service.DirectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("DirectionService")
@Transactional
public class DirectionServiceImpl extends BaseServiceImpl implements DirectionService {

    @Autowired
    DirectionRepository DirectionRepository;

    @Override
    public List<Direction> getDirections(int start, int length,Map<String,String> condition,String orderName,
                                      String dir) {
        Page<Direction> pages = null;
        Pageable pageable=null;
        if(condition.get("dataFilter").equals("所有")){
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getName(condition.get("key"), pageable);
        }else if(condition.get("dataFilter").equals("本部门")){
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getName(condition.get("key"), condition.get("superior"), pageable);
        }else if(condition.get("dataFilter").equals("本片区")){
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getNameBySaleArea(condition.get("key"), condition.get("areaId"), pageable);
        }
        else{
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getNameByUser(condition.get("key"), condition.get("userId"), pageable);
        }
        return pages.getContent();
    }

    @Override
    public long getDirectionsCount(Map<String,String> condition){
        long count=0;
        if(condition.get("dataFilter").equals("所有")){
            count =DirectionRepository.getCount(condition.get("key"));
        }else if(condition.get("dataFilter").equals("本部门")){
            count  = DirectionRepository.getCount(condition.get("key"), condition.get("superior"));
        }else if(condition.get("dataFilter").equals("本片区")) {
            count = DirectionRepository.getCountBySaleAreaId(condition.get("key"),condition.get("areaId"));
        }
        else {
            count = DirectionRepository.getCountByUser(condition.get("key"),condition.get("userId"));
        }
        return count;
    }

    //手机端页面显示
    @Override
    public List<Direction> getMobilePage(int start, int length,Map<String,Object> condition,String orderName,
                                         String dir) {
        Page<Direction> pages = null;
        Pageable pageable=null;
        if(condition.get("dataFilter").equals("所有")){
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getName((String)condition.get("key"), pageable);
        }else if(condition.get("dataFilter").equals("本部门")){
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getName((String)condition.get("key"), (String)condition.get("superior"), pageable);
        }else if(condition.get("dataFilter").equals("本片区")){
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getNameBySaleArea((String)condition.get("key"), (String)condition.get("areaId"), pageable);
        }
        else{
            pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
            pages = DirectionRepository.getNameByUser((String)condition.get("key"), (String)condition.get("userId"), pageable);
        }
        return pages.getContent();
    }

    @Override
    public long getMobilePageCount(Map<String,Object> condition){
        long count=0;
        if(condition.get("dataFilter").equals("所有")){
            count =DirectionRepository.getCount((String)condition.get("key"));
        }else if(condition.get("dataFilter").equals("本部门")){
            count  = DirectionRepository.getCount((String)condition.get("key"), (String)condition.get("superior"));
        }else if(condition.get("dataFilter").equals("本片区")) {
            count = DirectionRepository.getCountBySaleAreaId((String)condition.get("key"),(String)condition.get("areaId"));
        }
        else {
            count = DirectionRepository.getCountByUser((String)condition.get("key"),(String)condition.get("userId"));
        }
        return count;
    }


    public DirectionRepository getRepository(){
        return this.DirectionRepository;
    }

}
